/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;


/**
 * Class SwitchFile
 */
public class SwitchFile {

    private IFile file;
    private String baseName;
    private String fileExtension;
    private String location;

    /**
     * @param file
     */
    public SwitchFile(IFile file) {
        this.file = file;
        String[] parts = file.getName().split("\\.");
        if (parts != null && parts.length > 1) {
            fileExtension = parts[parts.length-1];
            baseName = file.getName().replaceAll("." + fileExtension, "");
        }
        else {
            fileExtension = "";
            baseName = file.getName();
        }
        location = getLocation();
    }

    /**
     * @return
     */
    public String getName() {
        return baseName + "." + fileExtension;
    }
    
    /**
     * @return
     */
    public String getBaseName() {
        return baseName;
    }

    /**
     * @return
     */
    public String getFileExtension() {
        return fileExtension;
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        SwitchFile other = (SwitchFile) obj;
        boolean sameLocation = other.getLocation().equals(getLocation());
        boolean samePath = other.getPath().equals(getPath());
        boolean sameName = other.getBaseName().equals(getBaseName());
        boolean sameExtension = other.getFileExtension().equals(getFileExtension());
        return sameLocation && samePath && sameName && sameExtension;
    }

    /**
     * 
     */
    protected void resetFile() {
        IProject project = file.getProject();
        this.file = project.getFile(location + "/" + baseName + "." + fileExtension);
    }
    
    /**
     * @param baseName
     */
    public void changeBaseName(String baseName) {
        this.baseName = baseName;
        resetFile();
    }

    /**
     * Returns the project relative location of the file in the project tree excluding the file itself
     * e.g <tt>src/main/java/org/foo</tt>
     * @return the project relative location of the file 
     */
    public String getLocation() {
        IResource parent = file.getParent();
        return parent.getProjectRelativePath().toString();
    }

    /**
     * Changes the location of this SwitchFile instance and returns the instance for convenience
     * @param location the new location to set
     */
    public SwitchFile changeLocation(String location) {
        this.location = location;
        resetFile();
        return this;
    }

    /**
     * @param fileExtension
     */
    public void changeFileExtension(String fileExtension) {
       this.fileExtension = fileExtension;
       resetFile();
    }

    /**
     * Returns the project relative path of the file in the project tree 
     * e.g src/main/java/org/foo/Test.java 
     * @return the project relative location of the file
     */
    public String getPath() {
        return file.getProjectRelativePath().toString();
    }
    
    /**
     * @return
     */
    public boolean exists() {
        return file.exists();   
    }

    /**
     * @return
     */
    public SwitchFile makeCopy() {
        return new SwitchFile(file);
    }

    /**
     * @return
     */
    protected IFile getIFile() {
        return file;
    }
    
    public IProject getProject() {
      return file.getProject();  
    }
}
